package org.jackysoft.security.config;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.jasig.cas.client.proxy.ProxyGrantingTicketStorage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.security.cas.ServiceProperties;
import org.springframework.security.cas.authentication.CasAssertionAuthenticationToken;
import org.springframework.security.cas.authentication.CasAuthenticationProvider;
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
import org.springframework.stereotype.Component;

@Component("casAuthenticationProvider")
public class MyCasAuthenticationProvider extends CasAuthenticationProvider {

	@Value("${cas.server}")
	private String casServer;
	
	@Resource
	private ProxyGrantingTicketStorage proxyGrantingTicketStorage;
	
	@Override
	@Resource
	public void setAuthenticationUserDetailsService(
			AuthenticationUserDetailsService<CasAssertionAuthenticationToken> authenticationUserDetailsService) {
		super.setAuthenticationUserDetailsService(authenticationUserDetailsService);
		
	}

	@Override@Resource
	public void setServiceProperties(ServiceProperties serviceProperties) {
		// TODO Auto-generated method stub
		super.setServiceProperties(serviceProperties);
	}

	@PostConstruct
	public void init(){
		this.setTicketValidator(ticketValidator());
		this.setKey("an_id_for_this_auth_provider_only");
	}
	
	
	@Bean
	public  org.jasig.cas.client.validation.Cas20ServiceTicketValidator ticketValidator(){
		org.jasig.cas.client.validation.Cas20ServiceTicketValidator c2t = 
				new org.jasig.cas.client.validation.Cas20ServiceTicketValidator(casServer);
		c2t.setProxyCallbackUrl(casServer+"proxy/callback");
		c2t.setProxyGrantingTicketStorage(proxyGrantingTicketStorage);
        return  c2t;
	}
	
	
}
